#include "linkfactory.h"
#include <QFile>
#include <QUrl>
#include <QDebug>

LinkFactory::LinkFactory(QObject *parent) :
    QObject(parent)
{
    QFile f("input.txt");
    if(f.open(QFile::ReadOnly)){
        while(!f.atEnd()){
            QUrl url(f.readLine().trimmed());
            if(url.isValid() && !url.scheme().isEmpty()){
                Link* l=new Link;
                l->setUrl(url);
                l->setState(Link::FREE);
                files.append(l);
            }
        }
        f.close();
    }else{
        qDebug()<<"Bledna nazwa pliku wejsciowego!";
    };
}

QVector<Link*> LinkFactory::getAvailLinks(){
    QVector<Link*> out;
    foreach(Link* l, files){
        if(l->state()==Link::FREE){
            out.append(l);
        }else if(l->state()==Link::ERR_SOCKET_ERROR){
            l->setState(Link::FREE);
        }
    }
    return out;
}

LinkFactory::~LinkFactory(){
    foreach(Link* l, files){
        delete l;
    }
    files.clear();
}

QVector<Link*> LinkFactory::getAllLinks(){
    return QVector<Link*>(files);
};
